<template>
  <!-- 添加或修改用户配置对话框 -->
  <el-dialog title="报价" :visible.sync="open" width="1100px" :close-on-click-modal="false">
    <el-table :data="tableData" border style="width: 100%" show-summary :summary-method="getSummaries">
      <el-table-column type="index" width="50">
      </el-table-column>
      <el-table-column prop="deviceName" :label="$locale('myDevice.determinedList.device')">
      </el-table-column>
      <el-table-column prop="deviceNum" :label="$locale('geWudevice.code')">
      </el-table-column>
      <el-table-column prop="priceRmb" sortable :label="$locale('student.add.Unit price (RMB)')">
      </el-table-column>
      <el-table-column prop="priceUsd" sortable :label="$locale('student.add.Unit price (USD)')">
      </el-table-column>
      <el-table-column prop="deviceTotal" sortable :label="$locale('geWudevice.quantity')">
      </el-table-column>
      <el-table-column prop="sumPriceRmb" sortable :label="$locale('student.add.Total price (RMB)')">
      </el-table-column>
      <el-table-column prop="sumPriceUsd" sortable :label="$locale('student.add.Total price (USD)')">
      </el-table-column>
    </el-table>
    <div slot="footer" class="dialog-footer">
      <el-button type="warning" plain icon="el-icon-download" @click="handleExport">
        {{ $locale('order.add.Download Excel') }}
      </el-button>
      <el-button type="warning" plain icon="el-icon-download" @click="handleExportPdf">
        {{ $locale('order.add.Download Pdf') }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getOrderDetail } from '@/api/order/inquiry';

export default {
  name: 'quotation',
  data() {
    return {
      open: false,
      tableData: [],
      countPriceRmb: '',
      countPriceUsd: '',
      id: ''
    };
  },
  created() {

  },
  methods: {
    /** 新增按钮操作 */
    handleUpdate(row) {
      this.open = true;
      this.id = row.id;
      getOrderDetail(row.id).then(({ data }) => {
        this.tableData = data.orderDetails;
        this.countPriceRmb = data.countPriceRmb;
        this.countPriceUsd = data.countPriceUsd;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('manage/order/exportExcel', {
        id: this.id
      }, `报价_${new Date().getTime()}.xlsx`);
    },
    /** 导出按钮操作 */
    handleExportPdf() {
      this.download('manage/order/exportPdf', {
        id: this.id
      }, `报价_${new Date().getTime()}.pdf`);
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '总价';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (index == 6 || index == 7) {
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              // if (!isNaN(value)) {
              //     return prev + curr;
              // } else {
              //     return prev;
              // }
              if (index == 6) return this.countPriceRmb;
              else if (index == 7) return this.countPriceUsd;
            }, 0);
            sums[index] += ' 元';
          } else {
            sums[index] = 'N/A';
          }
        }
      });

      return sums;
    }
  }
};
</script>

<style lang="scss" scoped>
.el-rate {
  line-height: 2;
}
</style>
